package com.wang.back.servlet;

import com.wang.common.pojo.ApiResponse;
import com.wang.common.servlet.BaseServlet;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.pojo.AAdmins;
import com.wang.users.pojo.AUsers;
import com.wang.users.service.IUsersDbService;
import com.wang.users.service.impl.UsersDbServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 第二个Servlet 只存储和管理员息息相关的信息 登陆后的操作
 *
 * @author wangshMac
 */
@WebServlet(urlPatterns = "/back/AdminsBackServlet")
public class AdminsBackServlet extends BaseServlet {
    private IUsersDbService usersDbService = new UsersDbServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /* 接收参数 */
        String method = request.getParameter("method");
        if ("main".equalsIgnoreCase(method)) {
            /* 登陆后首页 */
            main(request, response);
        } else if ("welcome".equalsIgnoreCase(method)) {
            /* 欢迎页面 */
            welcome(request, response);
        } else if ("logout".equalsIgnoreCase(method)) {
            /* 退出 */
            logout(request, response);
        } else if ("adminsList".equalsIgnoreCase(method)) {
            adminsList(request, response);
        } else if ("adminsInsert".equalsIgnoreCase(method)) {
            adminsInsert(request, response);
        } else if ("adminsInsertSubmit".equalsIgnoreCase(method)) {
            adminsInsertSubmit(request, response);
        } else if ("adminsUpdate".equalsIgnoreCase(method)) {
            adminsUpdate(request, response);
        } else if ("adminsUpdateSubmit".equalsIgnoreCase(method)) {
            adminsUpdateSubmit(request, response);
        }
    }

    /**
     * 退出
     *
     * @param request
     * @param response
     * @throws IOException
     */
    private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        /* 从session中移除 */
        session.removeAttribute("admins");
        session.removeAttribute("users");
        /* 说明已经登陆 */
        response.sendRedirect(request.getContextPath() + "/NoLoginServlet?method=adminsLogin");
    }

    /**
     * 欢迎页面
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void welcome(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /* 准备路径 */
        this.path = "/WEB-INF/jsp/back/admins/welcome.jsp";
        /* 页面跳转 */
        request.getRequestDispatcher(path).forward(request, response);
    }

    /**
     * 首页
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /* 准备路径 */
        this.path = "/WEB-INF/jsp/back/admins/main.jsp";
        /* 页面跳转 */
        request.getRequestDispatcher(path).forward(request, response);
    }

    /**
     * 管理员列表
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void adminsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /* 准备路径 */
        this.path = "/WEB-INF/jsp/back/admins/adminsList.jsp";

        /* 分页信息 */
        PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
        /* 搜索条件,生成公共的搜索条件 */
        Map<String, Object> condMap = this.proccedSearch(request);
        /* 查询数据库
         * 所有的数据以JSON的形式返回
         *  */
        ApiResponse<AAdmins> apiResponse = this.usersDbService.findCondListAdminsService(pageInfoUtil, condMap);
        /* 将结果存储到Request中 */
        request.setAttribute("response", apiResponse.toJSON());

        /* 页面跳转 */
        request.getRequestDispatcher(path).forward(request, response);
    }

    /**
     * 管理员添加(打开添加页面)
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void adminsInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /* 准备路径 */
        this.path = "/WEB-INF/jsp/back/admins/adminsInsert.jsp";
        ApiResponse<AAdmins> apiResponse = new ApiResponse<>();
        /* 将结果存储到Request中 */
        request.setAttribute("response", apiResponse.toJSON());
        /* 页面跳转 */
        request.getRequestDispatcher(path).forward(request, response);
    }

    /**
     * 管理员添加(打开添加提交)
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void adminsInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String email = request.getParameter("email");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String sex = request.getParameter("sex");

        /* 接收参数 */
        AAdmins admins = new AAdmins();

        admins.setEmail(email);
        admins.setName(name);
        admins.setPassword(password);
        admins.setPhone(phone);
        admins.setSex(Byte.parseByte(sex));
        ApiResponse<Object> dbResponse = this.usersDbService.saveOneAdminsService(admins);
        this.printOut(dbResponse.toJSON().toJSONString(), response);
    }

    /**
     * 管理员添加(打开更新页面)
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void adminsUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /* 准备路径 */
        this.path = "/WEB-INF/jsp/back/admins/adminsInfo.jsp";
        String id = request.getParameter("id");
        Map<String,Object> condMap = new HashMap<>();
        condMap.put("id", id);
        ApiResponse<AAdmins> apiResponse = this.usersDbService.findOneAdminsService(condMap);
        /* 将结果存储到Request中 */
        request.setAttribute("response", apiResponse.toJSON());
        String operType = request.getParameter("operType");
        if ("updateInfo".equalsIgnoreCase(operType)) {
            /* 修改个人信息 */
            this.path = "/WEB-INF/jsp/back/admins/adminsUpdate.jsp";
        }else if ("resetPassword".equalsIgnoreCase(operType)) {
            /* 重置密码 */
            this.path = "/WEB-INF/jsp/back/admins/adminsResetPass.jsp";
        }
        /* 页面跳转 */
        request.getRequestDispatcher(path).forward(request, response);
    }

    /**
     * 管理员添加(打开更新提交)
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void adminsUpdateSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Map<String,Object> condMap = new HashMap<>();
        condMap.put("id", id);
        ApiResponse<AAdmins> dbResAdminsService = this.usersDbService.findOneAdminsService(condMap);
        /* 接收参数 */
        AAdmins admins = dbResAdminsService.getDataOneJava();

        boolean updateFlag = false;
        String operType = request.getParameter("operType");
        if ("updateInfo".equalsIgnoreCase(operType)) {
            String email = request.getParameter("email");
            String name = request.getParameter("name");
            String phone = request.getParameter("phone");
            String sex = request.getParameter("sex");
            String status = request.getParameter("status");

            admins.setEmail(email);
            admins.setName(name);
            admins.setPhone(phone);
            admins.setSex(Byte.parseByte(sex));
            admins.setStatus(Byte.parseByte(status));
            updateFlag = true;
        }else if ("resetPassword".equalsIgnoreCase(operType)) {
            String password = request.getParameter("password");
            admins.setPassword(password);
            updateFlag = true;
        }
        ApiResponse<Object> dbResponse = new ApiResponse<>();
        if (updateFlag) {
            dbResponse = this.usersDbService.updateOneAdminsService(admins);
        }
        this.printOut(dbResponse.toJSON().toJSONString(), response);
    }
}
